package com.bear.atlassianmcp.oauth.mcp.tools;

import com.bear.atlassianmcp.oauth.config.OAuthProperties;
import com.bear.atlassianmcp.oauth.model.OAuthToken;
import com.bear.atlassianmcp.oauth.service.OAuthService;
import com.bear.atlassianmcp.oauth.strategy.AuthenticationStrategy;
import com.bear.atlassianmcp.oauth.strategy.AuthenticationStrategyFactory;
import com.bear.atlassianmcp.oauth.strategy.OAuthStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * OAuth 管理 MCP 工具
 * 提供 OAuth 授权、状态检查、重新授权等功能
 */
@Service
public class OAuthManagementTools {
    
    private static final Logger logger = LoggerFactory.getLogger(OAuthManagementTools.class);
    
    private final OAuthService oauthService;
    private final OAuthProperties oauthProperties;
    private final AuthenticationStrategyFactory strategyFactory;
    
    @Autowired
    public OAuthManagementTools(OAuthService oauthService, OAuthProperties oauthProperties,
                               AuthenticationStrategyFactory strategyFactory) {
        this.oauthService = oauthService;
        this.oauthProperties = oauthProperties;
        this.strategyFactory = strategyFactory;
    }
    
    /**
     * 启动 Jira OAuth 授权流程
     */
    @Tool(description = "启动 Jira OAuth 1.0a 授权流程，获取授权链接", returnDirect = true)
    public String authorizeJira(ToolContext toolContext) {
        logger.info("MCP Tool: Starting Jira OAuth authorization");

        try {
            if (!oauthProperties.getJira().isEnabled()) {
                return "❌ Jira OAuth 未启用。请在配置文件中启用 OAuth 并配置相关参数。";
            }
            String authorizationUrl = oauthService.getAuthUrl(OAuthToken.ServiceType.JIRA);
            return "请访问该链接完成授权：" + authorizationUrl;

        } catch (Exception e) {
            logger.error("Failed to authorize Jira OAuth via MCP: {}", e.getMessage());
            return "❌ Jira OAuth 授权失败。原因：" + e.getMessage() + 
                   "\n💡 请检查 OAuth 配置并确保 Jira 服务器可访问。";
        }
    }
    
    /**
     * 启动 Confluence OAuth 授权流程
     */
    @Tool(description = "启动 Confluence OAuth 1.0a 授权流程，获取授权链接", returnDirect = true)
    public String authorizeConfluence(ToolContext toolContext) {
        logger.info("MCP Tool: Starting Confluence OAuth authorization");
        
        try {
            if (!oauthProperties.getConfluence().isEnabled()) {
                return "❌ Confluence OAuth 未启用。请在配置文件中启用 OAuth 并配置相关参数。";
            }
            String authorizationUrl = oauthService.getAuthUrl(OAuthToken.ServiceType.CONFLUENCE);
            return "请访问该链接完成授权：" + authorizationUrl;

        } catch (Exception e) {
            logger.error("Failed to authorize Confluence OAuth via MCP: {}", e.getMessage());
            return "❌ Confluence OAuth 授权失败。原因：" + e.getMessage() + 
                   "\n💡 请检查 OAuth 配置并确保 Confluence 服务器可访问。";
        }
    }
}
